package cn.wh.org.service.impl;

import cn.wh.basic.exception.BusinessException;
import cn.wh.basic.service.impl.BaseServiceImpl;
import cn.wh.basic.util.BaiduAuditUtils;
import cn.wh.org.domain.Employee;
import cn.wh.org.domain.Shop;
import cn.wh.org.domain.ShopAuditLog;
import cn.wh.org.mapper.EmployeeMapper;
import cn.wh.org.mapper.ShopAuditLogMapper;
import cn.wh.org.mapper.ShopMapper;
import cn.wh.org.service.IEmployeeService;
import cn.wh.org.service.IShopService;
import cn.wh.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yaosang
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private IEmployeeService iEmployeeService;

    /**
     *店铺入驻逻辑
     *   一：参数空校验
     *     。。不能为空
     *   二：合法校验   @TODO 百度AI智能审核接口做
     *   三：判断是否曾经已经注册过
     *     1. 店铺不能被注册
     *     2.人是否被注册
     *       2.1 注册了但是没有绑定店铺
     *       2.2 注册了并且已经绑定了店铺
     *   四：保存数据
     *     shop
     *     employee  shopId
     *     反过来修改shop中的admin_id
     * @author wh
     *
     */
    @Override
    public void settlement(Shop shop) {
        //
        Employee admin = shop.getAdmin();
        //一：参数空校验
        if (StringUtils.isEmpty(shop.getName())
                ||StringUtils.isEmpty(shop.getAddress())
                ||StringUtils.isEmpty(shop.getTel())
        ){
            throw new BusinessException("不能为空");
        }
        //二：合法校验   @TODO 百度AI智能审核接口做
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("不能说脏话哟");
        }
        //三：判断是否曾经已经注册过
        //判断店铺名是否存在
        Shop shopTmp = shopMapper.loadByName(shop.getName());
        //如果存在则不能注册
        if (shopTmp != null){
            throw new BusinessException("店铺已被注册");
        }
        //判断管理员是否存在
        Employee empTmp = employeeMapper.loadByUsername(admin.getUsername());
        //如果存在且已有管理的店铺则不能注册
        if (empTmp != null && empTmp.getShop_id() != null){
            throw new BusinessException("此员工已是管理员");
        }
        //如果不存在则可以注册,并且保存到employee表
        if (empTmp == null){
            iEmployeeService.save(admin);
            empTmp = admin;
        }
        //四：保存数据
        //通过员工id来设置 店铺 管理员id
        shop.setAdminId(empTmp.getId());
        shopMapper.save(shop);
        //通过店铺id来设置管理的店铺id
        empTmp.setShop_id(shop.getId());
        //反过来修改shop中的admin_id
        iEmployeeService.update(empTmp);
    }

    /**
     * 通过
     * @param shopAuditLog
     */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        //修改状态
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(1);
        shopMapper.update(shop);
        //修改日志 @TODO
       /* Employee employee = new Employee();
        employee.setId(1L);
        shopAuditLog.setAuditManager(employee);
        shopAuditLogMapper.save(shopLog);*/
        //发送激活邮件
        //通过log的店铺id 得到店铺id 在通过店铺id 获得管理员信息
        Employee employee = employeeMapper.loadByShopId(shopAuditLog.getShopId());
        //发送电子邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("364190601@qq.com");
        message.setTo("364190601@qq.com");
        message.setSubject("宠物乐园-店铺入驻激活请求");
        message.setText("亲，您的店铺信息已经通过审核，请尽快完成激活并使用：" +
                "激活链接：http://localhost:8080/shop/activation/"+shopAuditLog.getShopId());
        javaMailSender.send(message);
    }
    /**
     * 驳回
     * @param shopAuditLog
     */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        //修改状态
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(2);
        shopMapper.update(shop);
        //修改日志 @TODO
        shopAuditLog.setAuditId(334L);//@TODO
        shopAuditLogMapper.save(shopAuditLog);
        //发送激活邮件
        //通过log的店铺id 得到店铺id 在通过店铺id 获得管理员信息
        Employee employee = employeeMapper.loadByShopId(shopAuditLog.getShopId());
        //发送电子邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("364190601@qq.com");
        message.setTo("364190601@qq.com");
        message.setSubject("宠物乐园-店铺入驻驳回文件");
        message.setText("亲，您的店铺信息有误,未通过审核，请尽快修改：" +
                "激活链接：http://localhost:8081/#/register2?shopId="+shopAuditLog.getShopId());
        javaMailSender.send(message);
    }

    @Override
    public List<ShopVo> queryEcharts() {
        return shopMapper.queryEcharts();
    }

    @Override
    public void activation(Long shopId) {
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(3);
        shopMapper.update(shop);
    }

    /**
     * 批量删除
     * @param list
     */
    @Override
    public void batchInsert(List<Shop> list) {
        shopMapper.batchInsert(list);
    }

    /**
     *
     * @param adminId
     */
    @Override
    public Shop findAdminById(Long adminId) {
        Shop shop = shopMapper.loadById(adminId);
        return shop;
    }

}
